package com.maschinenstuermer.profiler.model.impl;



import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;

import com.maschinenstuermer.profiler.model.TraceModelBuilder;


@XmlRootElement
public class MethodInvocation extends CompositeInvocation {
	@XmlElement
	private Metrics selfMetrics = new Metrics();
	
	private transient String methodName;

	private transient ProfiledMethod method;
	
	/**
	 * Required by JAXB.
	 */
	public MethodInvocation() {	
	}
	
	public MethodInvocation(String methodName) {
		this.methodName = methodName;		
	}
	
	public Metrics getSelfMetrics() {
		return selfMetrics;
	}

	public String getMethodName() {
		if (methodName == null) {
			methodName = getMethod().getName();
		}
		return methodName;
	}
	
	@XmlIDREF
	public ProfiledMethod getMethod() {
		if (method == null) {
			method = TraceModelBuilder.getMethod(methodName);
		}
		return method;
	}

	public void setMethod(ProfiledMethod profiledMethod) {
		this.method = profiledMethod;
	}
	
	@Override
	public void accept(ModelVisitor visitor) {
		visitor.visit(this);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((methodName == null) ? 0 : methodName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		MethodInvocation other = (MethodInvocation) obj;
		return getMethod().equals(other.getMethod());
	}
}
